const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

// 递归平铺
const flattenArray = (arr) => {
  return arr.flatMap(item => {
    return Array.isArray(item.children) ? [item, ...flattenArray(item.children)] : item;
  });
}

// 递归计算最大层级数
const getMaxNestingLevel = (data, level = 1) => {
  let maxLevel = level;
  data.forEach(item => {
    if (Array.isArray(item.children)) {
      const childrenLevel = getMaxNestingLevel(item.children, level + 1);
      maxLevel = Math.max(maxLevel, childrenLevel);
    }
  });
  return maxLevel;
}

module.exports = {
  formatTime,
  flattenArray,
  getMaxNestingLevel
}
